package com.auth.manager.security.login;

import com.auth.manager.common.resp.Result;
import com.auth.manager.utils.ResponseUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0.0
 * @Date: 2022/4/27 15:50
 * @Author ZhuYouBin
 * @Description 用户未登录时候, 如果访问资源, 则会进入到这个方法里面
 */
@Component
public class NotLoginEntryPoint implements AuthenticationEntryPoint {

    // 用户未登录时候,执行这个方法[可以返回提示信息]
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        // 设置状态码
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        ResponseUtil.response(response, Result.failed("登录已过期，请重新登录！"));
    }

}
